package cn.lg.soar.flow.biz.mapper;

import cn.lg.soar.flow.api.model.AuditLogDTO;
import cn.lg.soar.flow.biz.entity.SoarAuditLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 节点审批人
 * @author luguoxiang
 * @since 2025-08-07
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public interface SoarAuditLogMapper extends BaseMapper<SoarAuditLog> {

    @Select("SELECT a.ID_ AS taskId, a.TASK_DEF_KEY_ AS taskKey, a.NAME_ AS taskName, a.PROC_INST_ID_ AS procInstId, " +
            " a.START_TIME_ AS startTime, a.END_TIME_ AS endTime, a.DUE_DATE_ AS dueTime, b.user_id, b.result, b.comments " +
            "FROM act_hi_taskinst a LEFT JOIN lg_flow_audit_log b ON a.ID_=b.task_id " +
            "WHERE a.PROC_INST_ID_=#{procInstId} ORDER BY a.START_TIME_")
    List<AuditLogDTO> listByProcInstId(String procInstId);

    @Select("SELECT a.ID_ AS taskId, a.TASK_DEF_KEY_ AS taskKey, a.NAME_ AS taskName, a.PROC_INST_ID_ AS procInstId,  " +
            "a.START_TIME_ AS startTime, a.END_TIME_ AS endTime, a.DUE_DATE_ AS dueTime, b.user_id, b.result, b.comments " +
            "FROM act_hi_taskinst a INNER JOIN act_hi_procinst c ON a.PROC_INST_ID_=c.PROC_INST_ID_ " +
            "LEFT JOIN lg_flow_audit_log b ON a.ID_=b.task_id " +
            "WHERE c.PROC_DEF_ID_ LIKE #{flowCode} AND c.BUSINESS_KEY_=#{bizId} ORDER BY a.START_TIME_")
    List<AuditLogDTO> listByBiz(String flowCode, String bizId);

    @Select("SELECT TASK_ID_, USER_ID_ FROM act_ru_identitylink WHERE TASK_ID_ IN (${taskIds}) and TYPE_='candidate'")
    List<Map<String, String>> getCandidates(String taskIds);

}
